<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class adv_page extends CI_Controller{
      protected $_templates;
      function __construct() {
          parent::__construct();
          $this->pre_message = "";         
          $this->permit_lib->check_permit();
          $this->session->set_userdata(array('Url'=>uri_string()));
		  $this->lang->load('adv');
          $this->load->model('adv_page_model','adv_page');
      }   
	  function index(){
		  $this->list_adv_page();
	  }
      function list_adv_page($page=0){
         
            /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('adv_page_name'=>'Tên trang','adv_page_description'=>'Mô tả'),'adv_page/list_adv_page/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
            }else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
            /*End  search*/
            $data['title'] = 'Danh sách trang quảng cáo';
            $data['add'] = 'adv_page/add';
            
            $config['base_url'] = base_url().'adv_page/list_adv_page/';  
            $data['num'] = $this->adv_page->getNumAdvPage($arr_search);
            $config['total_rows']   =  $data['num'];
            $config['per_page']  =   '20';
            $config['uri_segment'] = 4;   
            $this->pagination->initialize($config);   
            $data['pagination']    = $this->pagination->create_links(); 
            
            $data['list'] =   $this->adv_page->getAllAdvPage($config['per_page'],(int)$page,$arr_search);
            $data['page']=$page;
            
            $this->_templates['page'] = 'adv_page/list';	
           if($type=="ajax")
		   {
                echo $this->load->view($this->_templates['page'],$data,true);
		   }else
                $this->site_lib->load($this->_templates['page'],$data); 
      }
      
      function add(){                         
            $data['title'] = 'Thêm mới trang';
            $this->form_validation->set_rules('adv_page_code',lang('adv_page_code'),'trim|required');
            $this->form_validation->set_rules('adv_page_name',lang('adv_page_name'),'trim|required');
			//$this->form_validation->set_rules('adv_page_description',lang('adv_page_description'),'trim|required');
			
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
                $data = $this->build_data($_POST,1);
                 if($this->adv_page->saveData(0,$data)){
                     $this->session->set_flashdata('message','Lưu thành công');
                     redirect('adv_page/list_adv_page');
                 }else{

                     $this->pre_message = 'Lưu không thành công';
                 }
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'adv_page/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){                         
            $data['title'] = 'Cập nhật trang';
            $data['item'] = $this->adv_page->getAdvPageByID($id);
            $this->form_validation->set_rules('adv_page_code',lang('adv_page_code'),'trim|required');
            $this->form_validation->set_rules('adv_page_name',lang('adv_page_name'),'trim|required');
			//$this->form_validation->set_rules('adv_page_description',lang('adv_page_description'),'trim|required');
			
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				
               
                $data = $this->build_data($_POST,0);
					
                if($this->adv_page->saveData($id,$data)){
                    $this->session->set_flashdata('message','Lưu thành công');
                    redirect('adv_page/list_adv_page');
                }else{
                    $this->pre_message = 'Lưu không thành công';
                }
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'adv_page/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del($id,$page=0){
        if($this->adv_page->delete($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('adv_page/list_adv_page/'.$page);
    }
    function dels(){
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->adv_page->delete($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('adv_page/list_adv_page/'.$page);
    }
	
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
        $dataUpdate["adv_page_code"]=trim($data["adv_page_code"]);
		$dataUpdate["adv_page_name"]=trim($data["adv_page_name"]);
		$dataUpdate["adv_page_description"]=trim($data["adv_page_description"]);

        if($flag==1){
            $dataUpdate["dt_create"]=date('Y-m-d H:s:i');
        }
		if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}
    
  }
?>
